<template>
  <div class="outbox">
    <div class="supbox">
      <page-title :has-line="false">{{ tit }}</page-title>
      <p-tabs
        v-model:value="activeTab"
        :is-show-num="true"
        :tab-list="tabList"
      ></p-tabs>
      <query-page
        v-model:selectedKeys="selectedKeys"
        :form-component-config="formComponentConfig"
        :form-render-config="formRenderConfig"
        :form-render-keys="formRenderKeys"
        :table-component-config="tableComponentConfig"
        :table-render-config="tableRenderConfig"
        :table-render-keys="tableRenderKeys"
        :loading="isLoading"
        :data="renderData"
        :total="100"
        height="calc(100vh - 270px)"
        row-selection-type="checkbox"
        @selection-change="selectionChange"
        @query-data="getData"
      >
        <template #header-ext-container>
          <a-radio-group
            v-model="activeRadio"
            type="button"
            class="radio-ara"
            @change="handleChangegroup"
          >
            <a-radio value="0">未指定</a-radio>
            <a-radio value="1">询价采购</a-radio>
            <a-radio value="2">单一来源采购</a-radio>
            <a-radio value="3">比选采购</a-radio>
            <a-radio value="4">竞争性谈判采购</a-radio>
            <a-radio value="5">招投标</a-radio>
          </a-radio-group>
        </template>
        <template #container-side>
          <batchBtn
            :btn-list="btnList"
            type="1"
            :other-params="{ activeTab, activeRadio, selectedKeys }"
            @result="rowClickResult"
          ></batchBtn>
        </template>
        <template #operate="{ record, rowIndex }">
          <batchBtn
            :other-params="{
              record,
              rowIndex,
              activeTab,
              activeRadio,
              tableRenderKeys2,
              renderData2,
              btnList2,
              popUpComflag,
              detailswitchflag,
              selectedKeys,
              batchBtnType,
            }"
            :thiscode="thiscode"
            type="3"
            @result="operateClickResult"
          ></batchBtn>
        </template>
      </query-page>
    </div>

    <!-- 批量上传 -->
    <batchImport
      v-if="batchVisible"
      :import-url="importurl"
      :export-url="exportUrl"
      :dispflag="displflag"
      @close-drawer="closeDrawer"
      @finish="finish"
    ></batchImport>

    <!-- 弹窗 -->
    <popUp
      v-if="popVisible"
      :title-text="popUpTitle"
      :content-text="popUptext"
      :pop-visibleflag="popVisible"
      @handleOk="handleOk"
      @handleCancel="handleCancel"
    >
      <template v-if="contentflag" #content>
        <!-- 驳回原因 -->
        <div v-if="rejectflag">
          <span>驳回原因</span>
          <a-space direction="vertical" size="large" fill>
            <a-textarea
              v-model="rejectReason"
              placeholder="最大输入1000字符"
              :max-length="{ length: 1000, errorOnly: true }"
              allow-clear
              show-word-limit
            />
          </a-space>
        </div>
        <!-- 选商方式： -->
        <div v-if="popupseleflag">
          <span>选商方式：</span>
          <dict-select
            v-model="form.test"
            style="width: 200px"
            allow-clear
            dict-type="businessSelectionStrategy"
            placeholder="请选择"
          ></dict-select>
        </div>
        <div v-if="attachmentmanagementflag">附件管理</div>
        <!-- 指定供应商编辑 -->
        <div v-if="competitivepopupflag">
          <a-form ref="formRef" :model="pushdata.form" :rules="formRules">
            <!-- 选商方式确认子表单新增+编辑 -->
            <a-row :gutter="24">
              <a-col :span="12">
                <a-form-item
                  field="name1"
                  label="是否采购方式变更："
                  label-col-flex="180px"
                >
                  <dict-select
                    v-model="pushdata.form.name1"
                    style="width: 200px"
                    allow-clear
                    dict-type="currencyType"
                    placeholder="请选择"
                  ></dict-select>
                </a-form-item>
                <a-form-item
                  field="name2"
                  label="选商方式："
                  label-col-flex="180px"
                >
                  <dict-select
                    v-model="pushdata.form.name2"
                    style="width: 200px"
                    allow-clear
                    dict-type="currencyType"
                    placeholder="请选择"
                  ></dict-select>
                </a-form-item>
                <a-form-item
                  field="name3"
                  label="计划编号："
                  label-col-flex="130px"
                >
                  <a-input
                    v-model="pushdata.form.name3"
                    placeholder=""
                    disabled
                  />
                </a-form-item>
                <a-form-item
                  field="name4"
                  label="物料名称："
                  label-col-flex="70px"
                >
                  <a-input
                    v-model="pushdata.form.name4"
                    placeholder=""
                    disabled
                  />
                </a-form-item>
                <a-form-item
                  field="name5"
                  label="牌号："
                  label-col-flex="130px"
                >
                  <a-input
                    v-model="pushdata.form.name5"
                    placeholder=""
                    disabled
                  />
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item
                  field="name6"
                  label="变更依据："
                  label-col-flex="170px"
                >
                  <a-input v-model="pushdata.form.name6" placeholder="" />
                </a-form-item>
                <a-form-item
                  field="name7"
                  label="机型："
                  label-col-flex="130px"
                >
                  <a-input
                    v-model="pushdata.form.name7"
                    placeholder=""
                    disabled
                  />
                </a-form-item>
                <a-form-item
                  field="name8"
                  label="物料编号："
                  label-col-flex="70px"
                >
                  <a-input
                    v-model="pushdata.form.name8"
                    placeholder=""
                    disabled
                  />
                </a-form-item>
                <a-form-item
                  field="name9"
                  label="技术条件："
                  label-col-flex="120px"
                >
                  <a-input
                    v-model="pushdata.form.name9"
                    placeholder=""
                    disabled
                  />
                </a-form-item>
                <a-form-item
                  field="name10"
                  label="规格："
                  label-col-flex="70px"
                >
                  <a-input
                    v-model="pushdata.form.name10"
                    placeholder=""
                    disabled
                  />
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <!-- 上传文件编辑 -->
        <div v-if="uploadfileeditingflag">
          <a-form ref="formRef2" :model="pushdata.form" :rules="formRules">
            <!-- 选商方式确认子表单新增+编辑 -->
            <a-row :gutter="24">
              <a-col :span="12">
                <a-form-item
                  field="planListNo"
                  label="计划编号："
                  label-col-flex="100px"
                >
                  <a-input
                    v-model="pushdata.form.planListNo"
                    disabled
                    placeholder=""
                  />
                </a-form-item>
                <a-form-item
                  field="mdsItemCode"
                  label="物料编号："
                  label-col-flex="100px"
                >
                  <a-input v-model="pushdata.form.mdsItemCode" placeholder="" />
                </a-form-item>
                <a-form-item
                  field="tndofrFileNo"
                  label="招标文件编号："
                  label-col-flex="130px"
                >
                  <a-input
                    v-model="pushdata.form.tndofrFileNo"
                    placeholder=""
                  />
                </a-form-item>
                <a-form-item
                  field="pmsMajorCatogoryCode"
                  label="采购类型"
                  label-col-flex="180px"
                >
                  <dict-select
                    v-model="pushdata.form.pmsMajorCatogoryCode"
                    style="width: 200px"
                    allow-clear
                    dict-type="procurementType"
                    placeholder="请选择"
                  ></dict-select>
                </a-form-item>
                <a-form-item
                  field="actTenderDate"
                  label="实际招标时间："
                  label-col-flex="130px"
                >
                  <a-date-picker
                    v-model="pushdata.form.actTenderDate"
                    :allow-clear="true"
                    value-format="YYYY-MM-DD"
                    style="width: 254px; margin-bottom: 20px"
                  />
                </a-form-item>
                <a-form-item
                  field="vendorBiddingDate"
                  label="竞标时间："
                  label-col-flex="130px"
                >
                  <a-date-picker
                    v-model="pushdata.form.vendorBiddingDate"
                    :allow-clear="true"
                    value-format="YYYY-MM-DD"
                    style="width: 254px; margin-bottom: 20px"
                  />
                </a-form-item>
                <a-form-item
                  field="bidNoticeSendTime"
                  label="中标通知书发出时间："
                  label-col-flex="150px"
                >
                  <a-date-picker
                    v-model="pushdata.form.bidNoticeSendTime"
                    :allow-clear="true"
                    value-format="YYYY-MM-DD"
                    style="width: 254px; margin-bottom: 20px"
                  />
                </a-form-item>
                <a-form-item
                  field="tenderDutyName"
                  label="招标负责人："
                  label-col-flex="130px"
                >
                  <!-- <inputModal
                    v-model="pushdata.form.tenderDutyName"
                    :label-value="pushdata.form.tenderDutyName"
                    :value-name="'name'"
                    :title="'招标负责人：'"
                    :type="'supplyName'"
                  /> -->
                  <searchItemUser
                    v-model="pushdata.form.tenderDutyName"
                    v-model:name="pushdata.form.tenderDutyName"
                    :title="'招标负责人：'"
                  />
                </a-form-item>
              </a-col>
              <a-col :span="12">
                <a-form-item
                  field="tenderTypeCode"
                  label="招标方式："
                  label-col-flex="180px"
                >
                  <dict-select
                    v-model="pushdata.form.tenderTypeCode"
                    style="width: 200px"
                    allow-clear
                    dict-type="biddingMethod"
                    placeholder="请选择"
                  ></dict-select>
                </a-form-item>
                <a-form-item
                  field="mdsItemName"
                  label="物料名称："
                  label-col-flex="130px"
                >
                  <a-input v-model="pushdata.form.mdsItemName" placeholder="" />
                </a-form-item>
                <a-form-item
                  field="tndofrFileName"
                  label="招标文件名称："
                  label-col-flex="130px"
                >
                  <a-input
                    v-model="pushdata.form.tndofrFileName"
                    placeholder=""
                  />
                </a-form-item>
                <a-form-item
                  field="planTenderDate"
                  label="预计招标时间："
                  label-col-flex="150px"
                >
                  <a-date-picker
                    v-model="pushdata.form.planTenderDate"
                    :allow-clear="true"
                    value-format="YYYY-MM-DD"
                    style="width: 254px; margin-bottom: 20px"
                  />
                </a-form-item>
                <a-form-item
                  field="vendorTenderEndDate"
                  label="供应商投标结束时间："
                  label-col-flex="150px"
                >
                  <a-date-picker
                    v-model="pushdata.form.vendorTenderEndDate"
                    :allow-clear="true"
                    value-format="YYYY-MM-DD"
                    style="width: 254px; margin-bottom: 20px"
                  />
                </a-form-item>
                <a-form-item
                  field="vendorBiddingAddress"
                  label="竞标地点："
                  label-col-flex="130px"
                >
                  <a-input
                    v-model="pushdata.form.vendorBiddingAddress"
                    placeholder=""
                  />
                </a-form-item>
                <a-form-item
                  field="tenderDeptName"
                  label="招标负责人部门："
                  label-col-flex="130px"
                >
                  <a-input
                    v-model="pushdata.form.tenderDeptName"
                    placeholder=""
                  />
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="16">
              <a-form-item field="notes" label="描述：" label-col-flex="80px">
                <a-input v-model="pushdata.form.notes" placeholder="" />
              </a-form-item>
            </a-row>
          </a-form>
        </div>
      </template>
    </popUp>

    <!-- 文件导入 -->
    <fileUpload
      v-if="fileValueflag"
      v-model="fileValue"
      btn-name="文件上传"
      type="2"
      btn-type="text"
      title="Excel导入"
      accept=".xlsx"
      :max-size="0.1"
      @restion="fileValueflagfun"
    />
  </div>
</template>

<script lang="ts" setup>
  import { ref, onMounted, watch } from 'vue';
  import { Message, Notification } from '@arco-design/web-vue';
  import {
    getPlanListForBill, // 选商方式 （0-未指定 3-比选采购 4-竞争性谈判采购 5-招投标）
    getPlanListForXjcg, // 选商方式 （1-询价采购）
    getPlanListForDyly, // 选商方式 （2-单一来源） getPlanListForDyly
    getPlanListForZtb, // 选商方式 （5-招投标）
    updateBill,
    doApproval,
    getTenderByPlanListNo,
    getTenderByPlanListNo2,
    updateInquiryStatus,
    obtainSupplier,
    batchSave,
    exportCpWzd,
    exportYclWzd,
    exportBzjWzd,
    exportDzjWzd,
    exportLzjWzd,
    exportDyly,
  } from '@/api/supplier/selective-quotient';
  import lodash from 'lodash';
  import pageTitle from '@/common/components/title/page-title.vue';
  import queryPage from '@/common/components/query-page/index.vue';
  import searchItemUser from '@/common/components/search-modal/user.vue';
  // import inputModal from '@/common/components/search-modal/input-modal.vue';
  import dayjs from 'dayjs';
  import dictSelect from '@/common/components/dict-select/index.vue';
  import batchImport from '@/common/components/batch-import/import-container.vue';
  import fileUpload from './components/file-upload-page/index.vue';
  import popUp from './components/pop-up.vue';
  import {
    formRenderConfig,
    formComponentConfig,
  } from './components/formComp/baseFormConfig';
  import {
    tableRenderConfig,
    tableComponentConfig,
  } from './components/tableComp/baseTableConfig';
  import { baseRenderKeys } from './components/baseData';
  import { getBtnList } from './components/baseBtn';
  import { getBtnList2 } from './components/baseBtn2';
  import batchBtn from './components/batchBtn.vue';
  import PTabs from './components/p-tabs.vue';

  const tit = ref('选商方式确认');
  const selectedKeys = ref<any[]>([]);
  const activeTab = ref<number>(2);
  const activeRadio = ref('');
  const popUpTitle = ref('');
  const popUptext = ref('');
  const rejectReason = ref('');
  const typeChangekey = ref('');
  const typeChangekeylength = ref(0);
  const form = ref({
    test: '',
  });
  const isLoading = ref(true); // 一级表格控制loading状态
  const batchVisible = ref(false); // 批量上传
  const contentflag = ref(false); // 弹窗slot控制
  const popVisible = ref(false); // 弹窗控制
  const rejectflag = ref(false); // 驳回原因
  const popupseleflag = ref(false); // 选商方式
  const popUpComflag = ref(false); // 表单单条信息编辑开关
  const detailswitchflag = ref(false); // 表单单条信息详情开关
  const attachmentmanagementflag = ref(false); // 附件管理
  const fileValueflag = ref(false); // 表单单条信息详情开关
  const competitivepopupflag = ref(false); // 指定选商方式弹窗编辑
  const uploadfileeditingflag = ref(false); // 上传文件编辑
  const displflag = ref(true); // 上传文件模板显示/隐藏
  const fileValue = ref({});
  const renderData = ref<any[]>([]);
  const renderData2 = ref<any[]>([]);
  const otherParamslist = ref<any[]>([]);
  const queryInfoList = ref<any>([]);
  const tabList = ref([
    { label: '成品', id: 2 },
    { label: '原材料', id: 3 },
    { label: '标准件', id: 1 },
    { label: '零组件', id: 4 },
    { label: '锻铸件', id: 5 },
  ]);
  // 渲染检索项
  const formRenderKeys = ref<any[]>([]);
  // 当前表格要渲染哪些值
  const tableRenderKeys = ref<any[]>([]);
  const tableRenderKeys2 = ref<any[]>([]);
  const btnList = ref<any[]>([]);
  const btnList2 = ref<any[]>([]);
  const thiscode = ref<string>('200');
  const formRef = ref<any>();
  const formRef2 = ref<any>();
  const pushdata = ref({
    form: {
      name1: '',
      name2: '',
      name3: '',
      name4: '',
      name5: '',
      name6: '',
      name7: '',
      name8: '',
      name9: '',
      name10: '',
      planListNo: '',
      mdsItemCode: '',
      tndofrFileNo: '',
      pmsMajorCatogoryCode: '',
      actTenderDate: '',
      vendorBiddingDate: '',
      bidNoticeSendTime: '',
      tenderDutyName: '',
      notes: '',
      tenderTypeCode: '',
      mdsItemName: '',
      tndofrFileName: '',
      planTenderDate: '',
      vendorTenderEndDate: '',
      vendorBiddingAddress: '',
      tenderDeptName: '',
    },
  });
  const formRules = ref({
    name1: [
      {
        required: true,
        message: '请选择是否采购方式变更',
        trigger: 'change',
      },
    ],
    name2: [
      {
        required: true,
        message: '请选择选商方式',
        trigger: 'change',
      },
    ],
    name6: [
      {
        required: true,
        message: '请输入变更依据',
        trigger: 'change',
      },
    ],
    planListNo: [
      {
        required: true,
        message: '请输入计划编号',
        trigger: 'change',
      },
    ],
    name13: [
      {
        required: true,
        message: '请输入招标文件编号',
        trigger: 'change',
      },
    ],
    tndofrFileNo: [
      {
        required: true,
        message: '请输入招标文件编号',
        trigger: 'change',
      },
    ],
    pmsMajorCatogoryCode: [
      {
        required: true,
        message: '请选择采购类型',
        trigger: 'change',
      },
    ],
    tenderTypeCode: [
      {
        required: true,
        message: '请输入招标方式',
        trigger: 'change',
      },
    ],
  });
  const batchBtnType = ref('selequot');
  const planListNoList = ref<any>([]);
  const importurl = ref('');
  const exportUrl = ref('');

  const fileValueflagfun = (flag: any) => {
    fileValueflag.value = flag;
  };

  const getData = async (queryInfo: object) => {
    queryInfoList.value = queryInfo;
    console.log('queryInfo', queryInfo, thiscode.value);
    console.log('上面tab值：', activeTab.value, '下面tab值', activeRadio.value);

    // 0-未指定 3-比选采购 4-竞争性谈判采购
    if (['0', '3', '4'].includes(activeRadio.value)) {
      getDataList(activeRadio.value, activeTab.value);
    }
    // 1-询价采购
    if (['1'].includes(activeRadio.value)) {
      getDataList2(activeTab.value);
    }
    // 2-单一来源
    if (['2'].includes(activeRadio.value)) {
      getDataList3(activeTab.value);
    }
    // 5-招投标
    if (['5'].includes(activeRadio.value)) {
      getDataList4(activeTab.value);
    }
  };

  const selectionChange = async (val: string[]) => {
    selectedKeys.value = val;
    console.log('selectedKeys', val, selectedKeys.value);
  };

  // 获取列表数据 选商方式 （0-未指定 3-比选采购 4-竞争性谈判采购 5-招投标）
  const getDataList = async (billPolicy: string, mdsItemType: any) => {
    renderData.value = [];
    isLoading.value = true;
    const { data } = await getPlanListForBill({
      pageNo: 1,
      pageSize: 10,
      billPolicy: billPolicy || '0',
      mdsItemType: mdsItemType || 2,
      ...queryInfoList.value,
    });
    renderData.value = data.list;
    isLoading.value = false;
  };

  // 获取列表数据 选商方式 （1-询价采购）
  const getDataList2 = async (queryInfo: any) => {
    renderData.value = [];
    isLoading.value = true;
    const { data } = await getPlanListForXjcg({
      pageNo: 1,
      pageSize: 10,
      billPolicy: '1',
      mdsItemType: queryInfo || 2,
      ...queryInfoList.value,
    });
    renderData.value = data.list;
    isLoading.value = false;
  };

  // 获取列表数据 选商方式 （2-单一来源）
  const getDataList3 = async (queryInfo: any) => {
    renderData.value = [];
    isLoading.value = true;
    const { data } = await getPlanListForDyly({
      pageNo: 1,
      pageSize: 10,
      billPolicy: '2',
      mdsItemType: queryInfo || 2,
      ...queryInfoList.value,
    });
    renderData.value = data.records;
    isLoading.value = false;
  };

  // 获取列表数据 选商方式 （5-招投标）
  const getDataList4 = async (queryInfo: any) => {
    renderData.value = [];
    isLoading.value = true;
    const { data } = await getPlanListForZtb({
      pageNo: 1,
      pageSize: 10,
      billPolicy: '5',
      mdsItemType: queryInfo || 2,
      ...queryInfoList.value,
    });
    renderData.value = data.records;
    isLoading.value = false;
  };

  // 初始化tab切换数据
  const initData = async () => {
    selectedKeys.value = [];
    const cfg: any = {
      1: 'standardPart',
      2: 'finishedProduct',
      3: 'rawMaterial',
      4: 'zeroCom',
      5: 'forgingAndCasting',
    };
    const renderKey = cfg[activeTab.value];
    formRenderKeys.value =
      lodash.get(baseRenderKeys, `${renderKey}.formRenderKeys`) || [];
    tableRenderKeys.value =
      lodash.get(baseRenderKeys, `${renderKey}.newTableRenderKeys`) || [];
    getDataList('0', activeTab.value);
    btnListChange();
  };

  // 获取键值方法
  const btnListChange = () => {
    btnList.value = getBtnList(
      JSON.stringify(activeTab.value),
      activeRadio.value
    );
  };

  // 批量上传
  const closeDrawer = (flag: boolean) => {
    console.log(flag, '关闭弹窗');
    batchVisible.value = flag;
  };

  // 批量上传后的完成事件
  const finish = async (response: any, secretLevel: string, flag: boolean) => {
    console.log(response, secretLevel, flag, '完成');
    batchVisible.value = flag;
    initData();
  };

  // 确认按钮接口
  const updateInquiryStatusapi = async (inquiryStatusid: number) => {
    const result = selectedKeys.value.map((id) => ({
      id,
      inquiryStatus: inquiryStatusid,
    }));
    const { data } = await updateInquiryStatus([...result]);
    Message.success(data);
    await activRadiochange();
  };

  const activRadiochange = async () => {
    console.log(activeRadio.value, activeTab.value);
    if (activeRadio.value === '1') {
      await getDataList2(activeTab.value);
    } else if (activeRadio.value === '2') {
      await getDataList3(activeTab.value);
    } else if (activeRadio.value === '5') {
      await getDataList4(activeTab.value);
    } else {
      await getDataList(activeRadio.value, activeTab.value);
    }
  };

  // 指定选商方式
  const updateBillapi = async (billPolicyed: any) => {
    const result = selectedKeys.value.map((id) => ({
      id,
      billPolicy: billPolicyed,
    }));
    // Object.values(result)
    const { data } = await updateBill([...result]);
    Message.success(data);
    console.log(
      'billPolicy',
      activeRadio.value,
      'mdsItemType',
      activeTab.value
    );
    await activRadiochange();
  };

  // 处理确定按钮的点击事件
  const handleOk = async (flag: boolean, done: any) => {
    if (popupseleflag.value) {
      // 指定选商方式
      console.log(form.value.test, '选商策略代码');
      await updateBillapi(form.value.test);
      popVisible.value = flag;
    } else if (rejectflag.value) {
      // 驳回
      console.log(rejectReason.value, '驳回的内容');
      if (typeChangekey.value === '112' && typeChangekeylength.value > 0) {
        if (rejectReason.value.length > 0 && rejectReason.value.length < 1000) {
          const result = selectedKeys.value.map((id) => ({
            id,
            rejectReason: rejectReason.value,
            businessStatus: 4,
          }));
          const { data } = await doApproval([...result]);
          Message.success(data);
          await getDataList(activeRadio.value, activeTab.value);
        } else {
          Message.warning('请重新填写驳回原因最多可输入1000字！');
        }
      }
      popVisible.value = flag;
      await activRadiochange();
    } else if (competitivepopupflag.value) {
      const val = await formRef.value.validate();
      if (!val) {
        popVisible.value = flag;
      } else {
        done(false);
      }
      await activRadiochange();
    } else if (uploadfileeditingflag.value) {
      const val = await formRef2.value.validate();
      if (!val) {
        const arr: any = { ...pushdata.value.form };
        arr.id = otherParamslist.value.id;
        arr.planListNo = otherParamslist.value.planListNo;
        await getTenderByPlanListNo2(arr);
        popVisible.value = flag;
      } else {
        done(false);
      }
      await activRadiochange();
    } else if (typeChangekey.value === '211') {
      await updateInquiryStatusapi(1);
      popVisible.value = flag;
    } else {
      popVisible.value = flag;
      await activRadiochange();
    }
    selectedKeys.value = [];
  };

  const handleCancel = (flag: boolean) => {
    popVisible.value = flag;
  };

  // 根据给定的值改变表格渲染键和按钮列表
  const handleChangegroup = async (val: any) => {
    thiscode.value = `${activeTab.value + activeRadio.value + val}`;
    selectedKeys.value = [];
    console.log('层级', thiscode.value, selectedKeys.value);

    switch (activeTab.value + activeRadio.value + val) {
      case '200': // 成品 未指定
        btnListChange();
        tableRenderKeys.value =
          baseRenderKeys.finishedProduct.newTableRenderKeys;
        popUpComflag.value = false;
        detailswitchflag.value = false;
        await getDataList('0', 2);
        break;
      case '211': // 成品 询价采购
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.finishedProduct.RenderKeys122;
        popUpComflag.value = true;
        detailswitchflag.value = false;
        await getDataList2(2);
        break;
      case '222': // 成品 单一来源
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.finishedProduct.RenderKeys133;
        popUpComflag.value = true;
        detailswitchflag.value = false;
        await getDataList3(2);
        break;
      case '233': // 成品 比选采购
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.finishedProduct.RenderKeys144;
        tableRenderKeys2.value = baseRenderKeys.finishedProduct.RenderKeys1444;
        popUpComflag.value = false;
        detailswitchflag.value = true;
        btnList2.value = getBtnList2(activeTab.value + activeRadio.value + val);
        await getDataList('3', 2);
        break;
      case '244': // 成品 竞争性谈判采购
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.finishedProduct.RenderKeys155;
        tableRenderKeys2.value = baseRenderKeys.finishedProduct.RenderKeys1555;
        btnList2.value = getBtnList2(activeTab.value + activeRadio.value + val);
        popUpComflag.value = false;
        detailswitchflag.value = true;
        await getDataList('4', 2);
        break;
      case '255': // 成品 招投标
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.finishedProduct.RenderKeys166;
        tableRenderKeys2.value = baseRenderKeys.finishedProduct.RenderKeys1666;
        btnList2.value = getBtnList2(activeTab.value + activeRadio.value + val);
        popUpComflag.value = false;
        detailswitchflag.value = true;
        await getDataList4(2);
        break;

      case '300': // 原材料 未指定
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.rawMaterial.newTableRenderKeys;
        popUpComflag.value = false;
        detailswitchflag.value = false;
        await getDataList('0', 3);
        break;
      case '311': // 原材料 询价采购
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.rawMaterial.RenderKeys222;
        popUpComflag.value = true;
        detailswitchflag.value = false;
        await getDataList2(3);
        break;
      case '322': // 原材料 单一来源采购
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.rawMaterial.RenderKeys233;
        popUpComflag.value = true;
        detailswitchflag.value = false;
        await getDataList3(3);
        break;
      case '333': // 原材料 比选采购
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.rawMaterial.RenderKeys244;
        tableRenderKeys2.value = baseRenderKeys.rawMaterial.RenderKeys2444;
        popUpComflag.value = false;
        detailswitchflag.value = true;
        btnList2.value = getBtnList2(activeTab.value + activeRadio.value + val);
        await getDataList('3', 3);
        break;
      case '344': // 原材料 竞争性谈判采购
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.rawMaterial.RenderKeys255;
        tableRenderKeys2.value = baseRenderKeys.rawMaterial.RenderKeys2555;
        btnList2.value = getBtnList2(activeTab.value + activeRadio.value + val);
        popUpComflag.value = false;
        detailswitchflag.value = true;
        await getDataList('4', 3);
        break;
      case '355': // 原材料 招投标
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.rawMaterial.RenderKeys266;
        tableRenderKeys2.value = baseRenderKeys.rawMaterial.RenderKeys2666;
        btnList2.value = getBtnList2(activeTab.value + activeRadio.value + val);
        popUpComflag.value = false;
        detailswitchflag.value = true;
        await getDataList4(3);
        break;

      case '100': // 标准件 未指定
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.standardPart.newTableRenderKeys;
        popUpComflag.value = false;
        detailswitchflag.value = false;
        await getDataList('0', 1);
        break;
      case '111': // 标准件 询价采购
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.standardPart.RenderKeys322;
        popUpComflag.value = true;
        detailswitchflag.value = false;
        await getDataList2(1);
        break;
      case '122': // 标准件 单一来源采购
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.standardPart.RenderKeys333;
        popUpComflag.value = true;
        detailswitchflag.value = false;
        await getDataList3(1);
        break;
      case '133': // 标准件 比选采购
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.standardPart.RenderKeys344;
        tableRenderKeys2.value = baseRenderKeys.standardPart.RenderKeys3444;
        popUpComflag.value = false;
        detailswitchflag.value = true;
        btnList2.value = getBtnList2(activeTab.value + activeRadio.value + val);
        await getDataList('3', 1);
        break;
      case '144': // 标准件 竞争性谈判采购
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.standardPart.RenderKeys355;
        tableRenderKeys2.value = baseRenderKeys.standardPart.RenderKeys3555;
        btnList2.value = getBtnList2(activeTab.value + activeRadio.value + val);
        popUpComflag.value = false;
        detailswitchflag.value = true;
        await getDataList('4', 1);
        break;
      case '155': // 标准件 招投标
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.standardPart.RenderKeys366;
        tableRenderKeys2.value = baseRenderKeys.standardPart.RenderKeys3666;
        btnList2.value = getBtnList2(activeTab.value + activeRadio.value + val);
        popUpComflag.value = false;
        detailswitchflag.value = true;
        await getDataList4(1);
        break;

      case '400': // 零组件 未指定
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.zeroCom.newTableRenderKeys;
        popUpComflag.value = false;
        detailswitchflag.value = false;
        await getDataList('0', 4);
        break;
      case '411': // 零组件 询价采购
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.zeroCom.RenderKeys422;
        popUpComflag.value = true;
        detailswitchflag.value = false;
        await getDataList2(4);
        break;
      case '422': // 零组件 单一来源采购
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.zeroCom.RenderKeys433;
        popUpComflag.value = true;
        detailswitchflag.value = false;
        await getDataList3(4);
        break;
      case '433': // 零组件 比选采购
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.zeroCom.RenderKeys444;
        tableRenderKeys2.value = baseRenderKeys.zeroCom.RenderKeys4444;
        popUpComflag.value = false;
        detailswitchflag.value = true;
        btnList2.value = getBtnList2(activeTab.value + activeRadio.value + val);
        await getDataList('3', 4);
        break;
      case '444': // 零组件 竞争性谈判采购
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.zeroCom.RenderKeys455;
        tableRenderKeys2.value = baseRenderKeys.zeroCom.RenderKeys4555;
        btnList2.value = getBtnList2(activeTab.value + activeRadio.value + val);
        popUpComflag.value = false;
        detailswitchflag.value = true;
        await getDataList('4', 4);
        break;
      case '455': // 零组件 招投标
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.zeroCom.RenderKeys466;
        tableRenderKeys2.value = baseRenderKeys.zeroCom.RenderKeys4666;
        btnList2.value = getBtnList2(activeTab.value + activeRadio.value + val);
        popUpComflag.value = false;
        detailswitchflag.value = true;
        await getDataList4(4);
        break;

      case '500': // 零组件 未指定
        btnListChange();
        tableRenderKeys.value =
          baseRenderKeys.forgingAndCasting.newTableRenderKeys;
        popUpComflag.value = false;
        detailswitchflag.value = false;
        await getDataList('0', 5);
        break;
      case '511': // 零组件 询价采购
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.forgingAndCasting.RenderKeys522;
        popUpComflag.value = true;
        detailswitchflag.value = false;
        await getDataList2(5);
        break;
      case '522': // 零组件 单一来源采购
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.forgingAndCasting.RenderKeys533;
        popUpComflag.value = true;
        detailswitchflag.value = false;
        await getDataList3(5);
        break;
      case '533': // 零组件 比选采购
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.forgingAndCasting.RenderKeys544;
        tableRenderKeys2.value =
          baseRenderKeys.forgingAndCasting.RenderKeys5444;
        popUpComflag.value = false;
        detailswitchflag.value = true;
        btnList2.value = getBtnList2(activeTab.value + activeRadio.value + val);
        await getDataList('3', 5);
        break;
      case '544': // 零组件 竞争性谈判采购
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.forgingAndCasting.RenderKeys555;
        tableRenderKeys2.value =
          baseRenderKeys.forgingAndCasting.RenderKeys5555;
        btnList2.value = getBtnList2(activeTab.value + activeRadio.value + val);
        popUpComflag.value = false;
        detailswitchflag.value = true;
        await getDataList('4', 5);
        break;
      case '555': // 零组件 招投标
        btnListChange();
        tableRenderKeys.value = baseRenderKeys.forgingAndCasting.RenderKeys566;
        tableRenderKeys2.value =
          baseRenderKeys.forgingAndCasting.RenderKeys5666;
        btnList2.value = getBtnList2(activeTab.value + activeRadio.value + val);
        popUpComflag.value = false;
        detailswitchflag.value = true;
        await getDataList4(5);
        break;

      default:
        console.error(
          'Invalid activeTab value:',
          activeTab.value + activeRadio.value + val
        );
    }
    console.log('tableRenderKeys.value:', tableRenderKeys.value);
  };

  // 行内列表操作列点击事件
  const operateClickResult = async (
    type: any,
    flagchange: boolean,
    otherParams: any,
    flag: boolean
  ) => {
    console.log(
      '行内列表操作列点击事件',
      type?.record?.id,
      flagchange,
      otherParams?.record,
      flag
    );

    if (type?.record?.id) {
      selectedKeys.value = [type.record.id];
    }
    console.log('========otherParams?.record,=============');
    console.log(otherParams?.record);
    console.log('====================================');
    otherParamslist.value = otherParams?.record;

    if (flag) {
      contentflag.value = true;
      popVisible.value = true;

      rejectflag.value = false;
      popupseleflag.value = false;
      attachmentmanagementflag.value = false;
      competitivepopupflag.value = false; // 指定选商方式弹窗编辑
      uploadfileeditingflag.value = true; // 上传文件编辑弹窗编辑
      console.log(
        otherParams?.record,
        otherParams?.record.planListNo,
        'otherParams?.record'
      );
      pushdata.value.form.planListNo = otherParams?.record.planListNo;
      await getTenderByPlanListNoapi(otherParams?.record?.planListNo);
    }
  };

  // 获取通过采购计划标识获得招投标uploadfileeditingflag
  const getTenderByPlanListNoapi = async (planListNoid: any) => {
    const { data } = await getTenderByPlanListNo(planListNoid);
    if (data) {
      pushdata.value.form = data;
      console.log(data, pushdata.value.form);
      console.log('=========data, pushdata.value.form=========');
    }
  };

  // 上面列表点击事件
  const rowClickResult = async (
    type: string,
    typeChange: string,
    rowParams: any
  ) => {
    console.log(
      '上面列表点击事件',
      type,
      typeChange,
      rowParams,
      rowParams.selectedKeys.length,
      renderData.value
    );
    typeChangekey.value = typeChange;
    typeChangekeylength.value = rowParams.selectedKeys.length;
    /**
     * contentflag 弹窗slot控制
     * popVisible 弹窗控制
     * rejectflag 驳回原因
     * popupseleflag 选商方式
     * attachmentmanagementflag 附件管理
     */
    if (rowParams.selectedKeys.length > 0) {
      renderData.value.forEach((item) => {
        rowParams.selectedKeys.forEach((item2: any) => {
          if (item.id === item2) {
            planListNoList.value.push(item.planListNo);
          }
        });
      });
      switch (typeChange) {
        case '111':
          popUpTitle.value = '指定选商方式';
          popUptext.value = '';
          contentflag.value = true;
          popVisible.value = true;

          rejectflag.value = false;
          popupseleflag.value = true;
          attachmentmanagementflag.value = false;
          competitivepopupflag.value = false; // 指定选商方式弹窗编辑
          uploadfileeditingflag.value = false; // 上传文件编辑弹窗编辑
          break;
        case '112':
          popUpTitle.value = '批量驳回';
          popUptext.value = '';
          contentflag.value = true;
          popVisible.value = true;

          rejectflag.value = true;
          popupseleflag.value = false;
          attachmentmanagementflag.value = false;
          competitivepopupflag.value = false; // 指定选商方式弹窗编辑
          uploadfileeditingflag.value = false; // 上传文件编辑弹窗编辑
          break;
        case '113':
          popUpTitle.value = '';
          popUptext.value = '';
          contentflag.value = false;
          popVisible.value = false;

          rejectflag.value = false;
          popupseleflag.value = false;
          attachmentmanagementflag.value = true;
          fileValueflag.value = true;
          competitivepopupflag.value = false; // 指定选商方式弹窗编辑
          uploadfileeditingflag.value = false; // 上传文件编辑弹窗编辑
          break;

        case '211':
          popUpTitle.value = '请确认';
          popUptext.value = '你确认要确认当前所选数据吗？';
          contentflag.value = true;
          popVisible.value = true;

          rejectflag.value = false;
          popupseleflag.value = false;
          attachmentmanagementflag.value = false;
          competitivepopupflag.value = false; // 指定选商方式弹窗编辑
          uploadfileeditingflag.value = false; // 上传文件编辑弹窗编辑
          break;
        case '213':
          try {
            Message.success('已修改到未指定！');
            await updateBillapi(0);
            planListNoList.value = [];
            selectedKeys.value = [];
          } catch (error) {
            await activRadiochange();
            planListNoList.value = [];
            selectedKeys.value = [];
          }
          break;
        case '311':
          try {
            obtainSupplier(planListNoList.value);
            Message.success('更新成功！');
            planListNoList.value = [];
            selectedKeys.value = [];
            await activRadiochange();
          } catch (error) {
            await activRadiochange();
            planListNoList.value = [];
            selectedKeys.value = [];
          }
          break;
        case '312':
          try {
            await batchSave(planListNoList.value);
            Message.success('操作成功!');
            planListNoList.value = [];
            selectedKeys.value = [];
            await activRadiochange();
          } catch (error) {
            console.log(error);
            planListNoList.value = [];
            selectedKeys.value = [];
            await activRadiochange();
          }
          break;
        case '198': // 竞争性谈判采购 指定选商方式弹窗
          popUpTitle.value = '';
          popUptext.value = '';
          contentflag.value = true; // 弹窗template开关
          popVisible.value = true; // 弹窗开关

          rejectflag.value = false;
          popupseleflag.value = false;
          attachmentmanagementflag.value = false;

          competitivepopupflag.value = true; // 指定选商方式弹窗编辑
          uploadfileeditingflag.value = false; // 上传文件编辑弹窗编辑
          break;
        case '614': // 确认生成选商结果
          try {
            await batchSave(planListNoList.value);
            Message.success('操作成功!');
            planListNoList.value = [];
            selectedKeys.value = [];
            await activRadiochange();
          } catch (error) {
            console.log(error);
            planListNoList.value = [];
            selectedKeys.value = [];
            await activRadiochange();
          }
          break;
        default:
          await activRadiochange();
          break;
      }
    } else {
      // ( 1标准件 2 成品 3原材料 4 零组件 5锻铸件)
      // （0-未指定 1-询价采购 2-单一来源 3-比选采购 4-竞争性谈判采购 5-招投标）
      switch (activeTab.value) {
        case 2:
          if (typeChange === '114') {
            await exportCpWzdapi(exportCpWzd, '成品未指定导出数据');
          } else if (typeChange === '115') {
            batchVisible.value = true;
            importurl.value =
              '/api/procurement/require/excelOperation/importCpWzd';
          } else if (typeChange === '116') {
            await exportCpWzdapi(exportDyly, '成品单一来源导出数据');
          } else if (typeChange === '117') {
            batchVisible.value = true;
            importurl.value =
              '/api/procurement/require/excelOperation/importDyly';
            exportUrl.value =
              'http://10.191.5.215:9000/yiyan/单一来源模板.xlsx';
          }
          break;
        case 3:
          if (typeChange === '114') {
            await exportCpWzdapi(exportYclWzd, '原材料未指定导出数据');
          } else if (typeChange === '115') {
            batchVisible.value = true;
            importurl.value =
              '/api/procurement/require/excelOperation/importYclWzd';
          } else if (typeChange === '116') {
            await exportCpWzdapi(exportDyly, '原材料单一来源导出数据');
          } else if (typeChange === '117') {
            batchVisible.value = true;
            importurl.value =
              '/api/procurement/require/excelOperation/importDyly';
          }
          break;
        case 1:
          if (typeChange === '114') {
            await exportCpWzdapi(exportBzjWzd, '标准件未指定导出数据');
          } else if (typeChange === '115') {
            batchVisible.value = true;
            importurl.value =
              '/api/procurement/require/excelOperation/importBzjWzd';
          } else if (typeChange === '116') {
            await exportCpWzdapi(exportDyly, '标准件单一来源导出数据');
          } else if (typeChange === '117') {
            batchVisible.value = true;
            importurl.value =
              '/api/procurement/require/excelOperation/importDyly';
          }
          break;
        case 4:
          if (typeChange === '114') {
            await exportCpWzdapi(exportLzjWzd, '零组件未指定导出数据');
          } else if (typeChange === '115') {
            batchVisible.value = true;
            importurl.value =
              '/api/procurement/require/excelOperation/importLzjWzd';
          } else if (typeChange === '116') {
            await exportCpWzdapi(exportDyly, '零组件单一来源导出数据');
          } else if (typeChange === '117') {
            batchVisible.value = true;
            importurl.value =
              '/api/procurement/require/excelOperation/importDyly';
          }
          break;
        case 5:
          if (typeChange === '114') {
            await exportCpWzdapi(exportDzjWzd, '锻铸件未指定导出数据');
          } else if (typeChange === '115') {
            batchVisible.value = true;
            importurl.value =
              '/api/procurement/require/excelOperation/importDzjWzd';
          } else if (typeChange === '116') {
            await exportCpWzdapi(exportDyly, '锻铸件单一来源导出数据');
          } else if (typeChange === '117') {
            batchVisible.value = true;
            importurl.value =
              '/api/procurement/require/excelOperation/importDyly';
          }
          break;
        default:
          console.error('Invalid activeTab.value');
      }
    }
  };

  // 导出
  const exportCpWzdapi = async (api: any, name: string) => {
    try {
      const res = (await api(queryInfoList.value)) as any;
      console.log(res);
      if (res.code === null || res.code === undefined) {
        const elink = document.createElement('a');
        elink.download = `${name}${dayjs()
          .format('YYYY-MM-DD_HH:mm:ss')
          .toString()}.xlsx`;
        elink.style.display = 'none';
        const blob = new Blob([res], { type: 'application/vnd.ms-excel' });
        console.log(blob);
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        document.body.removeChild(elink);
      } else {
        // 正常情况下文件的下载是不会返回retCode的，如果发现有返回码，说明就是接口出了问题
        Notification.error({
          title: '导出失败！',
          content: res.message,
        });
      }
    } catch (error) {
      // eslint-disable-next-line @typescript-eslint/no-unused-vars
      Notification.error({
        content: `出现异常！${error}`,
        closable: true,
        duration: 20000,
      });
    }
  };

  // 监听activeTab变化，更新状态和初始化数据
  watch(activeTab, () => {
    activeRadio.value = '0';
    initData();
  });

  watch(activeRadio, () => {
    if (activeRadio.value === '0') {
      displflag.value = false;
    } else {
      displflag.value = true;
    }
  });

  // 组件挂载时的初始化操作
  onMounted(() => {
    activeTab.value = 2;
    activeRadio.value = '0';
    initData();
    btnListChange();
  });
</script>

<style lang="less" scoped>
  .outbox {
    // width: 100%;
    // height: 100%;
    margin: 16px;
  }

  .supbox {
    // margin: 8px;
    padding: 0 24px 16px;
    background-color: white;
    border-radius: 6px;
  }

  .radio-ara {
    display: flex;
    justify-content: space-between;
    width: 100%;
  }

  :deep(.arco-radio-button) {
    width: 25%;
    text-align: center;
    border-radius: 2px;
  }

  .page-con {
    height: 100%;
    padding: 20px;
    background-color: #fff;
  }

  :deep(.arco-picker-size-medium) {
    margin: 0 !important;
  }
</style>
